package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import Communication.Client;
import Communication.MessageHandle;
import Controller.HandleCityController.HandleCityListener;
import View.HandleCityView;
import DB.CitiesQuery;
import Entity.Cities;


/** This class controls and manages the process that get the cities 
 * from the data base and also add and remove cities from DB
 */
public class HandleCityController implements Serializable{
	private static final long serialVersionUID = 42L;


	/**  Required attributes */
	static HandleCityView HandleCityView;
	Client client;
	CitiesQuery citiesModel;
	static Cities Cty;

	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public HandleCityController(Client client, WorkerMainMenuController workerMainMenuController) 
	{
		this.client = client;
		HandleCityView = new HandleCityView();
		HandleCityView.addHandleCityListener(new HandleCityListener(workerMainMenuController));
		getCites("addCity");
	}



	/**  Inner Listener class */ 
	class HandleCityListener implements ActionListener 
	{
		WorkerMainMenuController workerMainMenuController;

		public HandleCityListener(WorkerMainMenuController workerMainMenuController) 
		{
			this.workerMainMenuController = workerMainMenuController;
			citiesModel = new CitiesQuery();
			HandleCityView.showWindow();
		}

		/** Handle actions */
		public void actionPerformed(ActionEvent event) 
		{
			if (event.getSource() == HandleCityView.getbtnAdd()) 
			{
				if (citiesModel.isEmpty(HandleCityView.getCityID())
						||citiesModel.isEmpty(HandleCityView.getCityName()))
					SystemController.displayErrorMessage("One or more fields are Empty");
				else
				{
					Cty = new Cities(HandleCityView.getCityID(),HandleCityView.getCityName());		
					try {

						/**  send to server according to complete the  add process **/	
						MessageHandle msg = new MessageHandle(Cty, MessageHandle.ADD_CITY, false);
						client.sendToServer(msg);

					} catch (Exception e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace( );
						HandleCityView.hideWindow();
					}

				}


			}
			else if (event.getSource() == HandleCityView.getbtnDelete()) 
			{
				if (citiesModel.isEmpty(HandleCityView.getCityID())
						||citiesModel.isEmpty(HandleCityView.getCityName()))
					SystemController.displayErrorMessage("One or more fields are Empty");
				else
				{
					Cty=new Cities(HandleCityView.getCityID(),HandleCityView.getCityName());
					try {

						/**  send to server according to complete the delete process **/	
						MessageHandle msg = new MessageHandle(Cty, MessageHandle.DELETE_CITY, false);

						client.sendToServer(msg);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace( );
						HandleCityView.hideWindow();
					}
				}

			}

			else if (event.getSource() == HandleCityView.getbtnCancle()) 
			{
				HandleCityView.hideWindow();
				workerMainMenuController.showWindow();
				HandleCityView.clearFields();
			}
			HandleCityView.clearFields();
		}
	}

	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg){
		if (msg.compareTo("new city")==0)
			SystemController.displayMessage("New City added");
		else  if(msg.compareTo("exist city")==0) 
			SystemController.displayErrorMessage(" Add City Failed:The City already exists");
		else if(msg.compareTo("not exist city")==0)
			SystemController.displayErrorMessage(" Delete City Failed:The City not exists");
		else SystemController.displayMessage(" City Removed");

	}


	/**
	 * this function gets the cities list that in the data base right now
	 * @param message
	 */
	private void getCites(String message) {
		MessageHandle msg = new MessageHandle(null, MessageHandle.GET_CITIES, false);
		msg.setMsg(message);
		try {
			/**  send to server according to complete the process */	
			client.sendToServer(msg);
		} catch (IOException e) {
			System.out.print("HandleCityController->GetCities");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
